﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Xml;
using ISHotelApp.Database;

namespace ISHotelApp.XML
{
    public class Export2XML
    {
        public static void exportHost(object dataSource)
        {
            XmlTextWriter writer = null;
            try
            {
                DateTime currentDateTime = DateTime.Now;
                String dateStr = currentDateTime.ToString("yyyy-MM-dd HH_mm_ss");

                //writer = new XmlTextWriter("Hoste_" + dateStr + ".xml", Encoding.Unicode);
                writer = new XmlTextWriter("Hoste.xml", Encoding.Unicode);
                writer.Formatting = Formatting.Indented;
                writer.WriteStartDocument();
                writer.WriteStartElement("Hoste");

                //HostTable hostTable = new HostTable();
                Collection<Host> hosts = dataSource as Collection<Host>;

                foreach (var host in hosts)
                {
                    MyHost myHost = new MyHost();
                    myHost.Rodne_cislo = host.rodne_cislo;
                    myHost.Jmeno = host.jmeno;
                    myHost.Prijmeni = host.prijmeni;
                    myHost.Ulice = host.ulice;
                    myHost.Mesto = host.mesto;
                    myHost.PSC = host.psc;
                    myHost.Telefon = host.telefon;
                    myHost.Email = host.email;
                    myHost.ZapisDoXml(writer);
                }
            }
            catch(Exception ex)
            {
                throw ex;
            }
            if (writer != null) writer.Close();
        }

        public struct MyHost
        {
            public string Rodne_cislo;
            public string Jmeno;
            public string Prijmeni;
            public string Ulice;
            public string Mesto;
            public int PSC;
            public int Telefon;
            public string Email;


            public void ZapisDoXml(XmlTextWriter writer)
            {
                writer.WriteStartElement("Host");
                writer.WriteElementString("Rodne_cislo", Rodne_cislo);
                writer.WriteElementString("Jmeno", Jmeno);
                writer.WriteElementString("Prijmeni", Prijmeni);
                writer.WriteElementString("Ulice", Ulice);
                writer.WriteElementString("Mesto", Mesto);
                writer.WriteElementString("PSC", PSC.ToString());
                writer.WriteElementString("Telefon", Telefon.ToString());
                writer.WriteElementString("E-mail", Email);
                writer.WriteEndElement();
            }
        }
    }
}
